//  (C) Copyright  2009. 

//  Use, modification and distribution are subject to the 
//  General Public License, Version 3. (See http://www.gnu.org/licenses/licenses.html#GPL)

// Steel Assault 0.3.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

#ifdef _DEBUG
#include <crtdbg.h>
#endif

#define DIRECTINPUT_VERSION 0x0800
#define KEYDOWN(name, key) (name[key] & 0x80)

static const int INPUT_BUFFER_SIZE = 8;

#include <ctime>
#include <tchar.h>
#include <windows.h>
#include <commctrl.h>
#include <basetsd.h>
#include <dinput.h>
#pragma warning( disable : 4996 )
#include <strsafe.h>
#pragma warning( default : 4996 )

HWND MainWindowHandle=0;

bool InitWindowsApp(HINSTANCE instanceHandle,int show);

int Run(HINSTANCE instanceHandle)
{
	HRESULT hr = -1;
	MSG msg;
	DWORD dwItems=0,server_data_index = 0;
	float currTime=0.0,timeDelta=0.0;
	try
	{
		while(true)
		{
			if(::PeekMessage(&msg,0,0,0,PM_REMOVE))
			{
				if(msg.message==WM_QUIT)
					break;
				::TranslateMessage(&msg);
				::DispatchMessage(&msg);
			}
			else
			{
				//Game cycle
			}
		}
	}
	catch(...)
	{
		::MessageBox(0, "Unknown exception raised", ":(", MB_OK);
		return -1;
	}
	return 0;
}

LRESULT CALLBACK WndProc(HWND windowHandle,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:	
		::PostQuitMessage(0);
		return 0;
	}
	return ::DefWindowProc(windowHandle,msg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR pCmdLine,int nShowCmd)
{
	if(!InitWindowsApp(hInstance, nShowCmd))
		return -1;
	int result=Run(hInstance);
//calls to _CrtDumpMemoryLeaks are removed during preprocessing in release
	if (_CrtDumpMemoryLeaks())
	{
		::MessageBox(0, "Memory leaks!", ":(", MB_OK);
	}
	else
	{
		::MessageBox(0, "No memory leaks!", ":)", MB_OK);
	}

	return result;
}

bool InitWindowsApp(HINSTANCE instanceHandle, int show)
{
	WNDCLASS wc;
	wc.style         = CS_HREDRAW|CS_VREDRAW;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = instanceHandle;
	wc.hIcon         = ::LoadIcon(0,IDI_APPLICATION);
	wc.hCursor       = ::LoadCursor(0,IDC_ARROW);
	wc.hbrBackground =static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH));
	wc.lpszMenuName  = 0;
	wc.lpszClassName = "Steel Storm - Client";
	if(!::RegisterClass(&wc))
	{
		::MessageBox(0,"Failed to register window class",0,0);
		return false;
	}
	MainWindowHandle=::CreateWindow("Steel Storm - Client","Steel Storm - Client",WS_OVERLAPPEDWINDOW,
					CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,instanceHandle,0);
	if(MainWindowHandle==0)
	{
		::MessageBox(0,"CreateWindow - Failed",0,0);
		return false;
	}
	::ShowWindow(MainWindowHandle,show);
	::UpdateWindow(MainWindowHandle);
	return true;
}